package com.zuowei.ycy.user.ui.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Looper;
import android.view.View;

import com.and.yzy.frame.config.HttpConfig;
import com.and.yzy.frame.update.AppUpdateUtil;
import com.and.yzy.frame.update.UpdateCallBack;
import com.and.yzy.frame.util.RetrofitUtils;
import com.and.yzy.frame.view.dialog.MaterialDialog;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.AppConfig;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.UserInfo;
import com.zuowei.ycy.user.http.Article;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import butterknife.OnClick;
import okhttp3.FormBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 系统设置
 */
public class MineSystemSettingAty extends BaseToolbarAty {


    @Override
    public int getLayoutId() {
        return R.layout.mine_system_setting_layout;
    }

    @Override
    public void initData() {

        mToolbar.setTitle("设置");


    }



    @Override
    public void requestData() {


    }

    @OnClick({R.id.rl_us_phone, R.id.rl_about_us, R.id.rl_pinjia, R.id.rl_yijian,R.id.tv_commit,R.id.rl_update})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.rl_update:

                FormBody formBody = new FormBody.Builder().add("type", "1").build();

                AppUpdateUtil updateUtil = new AppUpdateUtil(this, HttpConfig.UPDATE_URL, formBody);

                updateUtil.checkUpdate(new UpdateCallBack() {
                    @Override
                    public void onError() {

                    }

                    @Override
                    public void isUpdate(String result) {

                    }

                    @Override
                    public void isNoUpdate() {
                        Looper.prepare();
                        showToast("已是最新版");
                        Looper.loop();
                    }
                });


                break;
            case R.id.rl_us_phone:


                showLoadingDialog(null);


                doHttp(RetrofitUtils.createApi(Article.class).setPage(), 1);



                break;
            case R.id.rl_about_us:
                startActivity(MineAboutUsAty.class, null);
                break;
            case R.id.rl_yijian:
                startActivity(MineYiJianAty.class, null);
                break;
            case R.id.tv_commit:

                Intent sIntent = new Intent(AppConfig.ACTION_LOGOUT);
                sendBroadcast(sIntent);

                UserManger.setIsLogin(false);
                UserManger.setUserInfo(new UserInfo());
                finish();
                break;
            case R.id.rl_pinjia:

                try {
                    Intent intent = new Intent();
                    Uri uri = Uri.parse("market://details?id=" + getPackageName());
                    intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("您还没有安装应用宝");
                }


                break;

        }
    }


    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {

        final String phone= AppJsonUtil.getString(result,"service_line");



        new MaterialDialog(this).setMDMessage("是否立即拨打客服电话?")
                .setMDConfirmBtnClick(new MaterialDialog.DialogBtnCallBack() {
                    @Override
                    public void dialogBtnOnClick() {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                        startActivity(intent);
                    }
                }).show();

        super.onSuccess(result, call, response, what);
    }
}
